defmodule Pango.NodeConfig do
	@moduledoc false

	require Logger

	def cluster_child(name) do
		{Cluster.Supervisor, [AppConfigure.fetch(:hosts), [name: name]]}
	end


	def topologies() do
		[
			cluster: [
				strategy: Cluster.Strategy.Epmd,
				config: [
					hosts: AppConfigure.fetch(:hosts)
				],
				topologies: {:net_kernel, :connect_node, []},
				disconnect: {:erlang, :disconnect_node, []},
				list_nodes: {:erlang, :nodes, [:connected]},
			]
		]
	end

	def connect() do
		Enum.each(AppConfigure.fetch(:topologies)[:cluster][:config][:hosts], &Node.connect(&1))
	end

	def set_node_info() do
		Logger.info("-----------------RUN_MODE:#{AppConfigure.fetch("RUN_MODE")}--------------------")
		bolt = AppConfigure.fetch(:bolt)
		bolt != nil && Logger.info("bolt: #{Keyword.get(bolt, :url)}")
		postgres = AppConfigure.fetch(:postgres)
		postgres != nil && Logger.info("postgres: #{Keyword.get(postgres, :hostname)}")
		case !AppConfigure.is_prod() do
			true ->
				node_ = AppConfigure.fetch(:node)
				cookie = AppConfigure.fetch(:cookie)
				Node.start(node_)
				Node.set_cookie(cookie)
			_ ->
				:skip
		end
	end

end
